<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <i class="el-icon-s-custom"></i>
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="账号"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="Caps lock is On"
        placement="right"
        manual
      >
        <el-form-item prop="password">
          <span class="svg-container">
            <i class="iconfont icon-mima"></i>
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <i
              class="iconfont"
              :class="
                passwordType === 'password' ? 'icon-xianshi' : 'icon-xianshi1'
              "
            />
          </span>
        </el-form-item>
      </el-tooltip>
      <!-- <el-form-item prop="captchaCode">
        <span class="svg-container">
          <i class="el-icon-s-custom"></i>
        </span>
        <el-input
          ref="captchaCode"
          v-model="loginForm.captchaCode"
          placeholder="验证码"
          name="captchaCode"
          style="width: calc(100% - 160px);"
          type="text"
          tabindex="1"
          autocomplete="on"
          @keyup.enter.native="handleLogin"
        />
         <img :src="captchaImg" class="captchaImg" @click="getCaptchaImg">
      </el-form-item> -->
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
        >Login</el-button
      >

      <div style="position: relative">
        <div class="tips">
          <span>Username : admin</span>
          <span>Password : 123456</span>
        </div>
        <div class="tips">
          <span style="margin-right: 18px">Username : editor</span>
          <span>Password : 123456</span>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { reqGetCaptcha, reqLogin } from '@/api/basics'
import { setToken } from '@/utils/auth'
// import { IS_OK } from '@/api/path'
export default {
  name: "Login",
  // components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位"));
      } else {
        callback();
      }
    };
    // const validateCaptchaCode = (rule, value, callback) => {
    //   if (value.length != 4) {
    //     callback(new Error("验证码错误"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      loginForm: {
        username: "18588773301",
        password: "123456"
        // captchaCode:""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ]
        // captchaCode: [
        //   { required: true, trigger: "blur", validator: validateCaptchaCode },
        // ],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      captchaImg: '',
      captchaToken: ''
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // this.getCaptchaImg()
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    //  async getCaptchaImg() {
    //   const res = await reqGetCaptcha()
    //   if (res.data.code === IS_OK) {
    //     this.captchaImg = res.data.data.captchaImg
    //     this.captchaToken = res.data.data.token
    //     this.loginForm.captchaCode = ''
    //   }
    // },
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const params = {
            username: this.loginForm.username,
            password: this.loginForm.password,
            // code: this.loginForm.captchaCode,
            token: this.captchaToken
          }
          await this.$store.dispatch("user/login", params)
          //  const res = await reqLogin(params)
          //    if (res.data.code === IS_OK) {
              //  setToken(res.data.data)
               setToken("resgssbyjj")
            //  }
           this.loading = false;
           this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
            // .then(() => {
            //   this.$router.push({
            //     path: this.redirect || "/",
            //     query: this.otherQuery,
            //   });
            //   this.loading = false;
            // })
            // .catch(() => {
            //   this.loading = false;
            // });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 获取其他参数
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 40px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 0px 5px 0px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
.captchaImg {
  width: 120px;
  height: 40px;
  float: right;
  border-radius: 4px;
  cursor: pointer;
}
</style>
